package cn.easybuy.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.easybuy.dao.BaseDao;
import cn.easybuy.dao.CategoryDao;
import cn.easybuy.entity.Category;

public class CategoryDaoImpl extends BaseDao implements CategoryDao {

	@Override
	public List<Category> getyiCategories() {
		List<Category> list = new ArrayList<Category>();
		ResultSet rs = null;
		String sql = "SELECT * FROM `easybuy_product_category` WHERE `type`=1";
		rs = this.exxcuteQuery(sql);
		try {
			while (rs.next()) {
				Category category = new Category();
				category.setId(rs.getInt("id"));
				category.setName(rs.getString("name"));
				category.setParentId(rs.getInt("parentId"));
				category.setType(rs.getInt("type"));
				category.setIconClass(rs.getString("iconClass"));
				list.add(category);

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closeAll(connection, pstmt, rs);
		}
		return list;
	}

	@Override
	public List<Category> geterCategories(int parentId) {
		List<Category> list = new ArrayList<Category>();
		ResultSet rs = null;
		String sql = "SELECT * FROM `easybuy_product_category` WHERE `type`=2 and parentId=?";
		rs = this.exxcuteQuery(sql, parentId);
		try {
			while (rs.next()) {
				Category category = new Category();
				category.setId(rs.getInt("id"));
				category.setName(rs.getString("name"));
				category.setParentId(rs.getInt("parentId"));
				category.setType(rs.getInt("type"));
				category.setIconClass(rs.getString("iconClass"));
				list.add(category);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closeAll(connection, pstmt, rs);
		}
		return list;
	}

	@Override
	public List<Category> getsanCategories(int parentId) {
		List<Category> list = new ArrayList<Category>();
		ResultSet rs = null;
		String sql = "SELECT * FROM `easybuy_product_category` WHERE `type`=3 and parentId=?";
		rs = this.exxcuteQuery(sql, parentId);
		try {
			while (rs.next()) {
				Category category = new Category();
				category.setId(rs.getInt("id"));
				category.setName(rs.getString("name"));
				category.setParentId(rs.getInt("parentId"));
				category.setType(rs.getInt("type"));
				category.setIconClass(rs.getString("iconClass"));
				list.add(category);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closeAll(connection, pstmt, rs);
		}
		return list;
	}

}
